{ "cells": [ { "cell_type": "markdown", "id": "d240914c", "metadata": {}, "source": [ "## Function Defination" ] }, { "cell_type": "markdown", "id": "7a1d0d06", "metadata": {}, "source": [ "- Positional arguments \n", " - by default ones\n", " - def foo(a,b)\n", " - foo(2,3) # Here a and b are positional arguments and you need to pass both..If u miss even one,error will come\n", " - means,the order in which we pass values when we call the function gets assigned its arguments respectively\n", "- Keyword arguments\n", " - you specify it explicitly using = symbol\n", " - def foo(a,b='sahil')\n", " - foo(2,3) # Here a is positional but b is keyword argument so is optional and you need to pass both..If u miss even one,error will come\n", " \n", "- Note: Keyword arguments should always be in the last during function declaration" ] }, { "cell_type": "markdown", "id": "00b3f2ce", "metadata": {}, "source": [ "### Make any argument as optional" ] }, { "cell_type": "code", "execution_count": 12, "id": "a7a03d7e", "metadata": {}, "outputs": [], "source": [ "def hi_random_person(name,age=None):\n", " print('Hi',name)\n", " if age:\n", " print('I am '+ str(age)+' years old')" ] }, { "cell_type": "code", "execution_count": 13, "id": "e95a80f0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hi sahil\n" ] } ], "source": [ "hi_random_person('sahil')" ] }, { "cell_type": "code", "execution_count": 14, "id": "723795c8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hi sourav\n", "I am 24 years old\n" ] } ], "source": [ "hi_random_person('sourav',24)" ] }, { "cell_type": "markdown", "id": "4da6dcef", "metadata": {}, "source": [ "### Set a default value for a argument" ] }, { "cell_type": "code", "execution_count": 39, "id": "498f7ee7", "metadata": {}, "outputs": [], "source": [ "def hi(name,place='Jammu'): # note: here name is called positional argument and place is called keyword argument\n", " print('Hi ' + name+ \" from \"+ place)" ] }, { "cell_type": "code", "execution_count": 28, "id": "3de8ddb6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hi sahil from Jammu\n" ] } ], "source": [ "hi('sahil')" ] }, { "cell_type": "code", "execution_count": 29, "id": "68d1b1da", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hi sourav from Mumbai\n" ] } ], "source": [ "hi('sourav','Mumbai')" ] }, { "cell_type": "markdown", "id": "f19ab5d3", "metadata": {}, "source": [ "#### Use case" ] }, { "cell_type": "code", "execution_count": 48, "id": "0cf41622", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "3\n", "1\n" ] } ], "source": [ "# Problem\n", "def add(a,b):\n", " return a+b\n", "\n", "print(add(1,2))\n", "#print(add(1)) # it will throw error\n", "\n", "# Solution\n", "def add(a,b=0): # or b=None\n", " return a+b\n", "\n", "print(add(1,2))\n", "print(add(1)) # it will throw error" ] }, { "cell_type": "markdown", "id": "7de536a2", "metadata": {}, "source": [ "### When you are unsure about the number of arguments - For positional arguments" ] }, { "cell_type": "markdown", "id": "0ca1da7e", "metadata": {}, "source": [ "- Use single asterick\n", "- *args\n", "- Note: it picks all the arguments after the positional arguments are assigned\n", "- args is tupple by default" ] }, { "cell_type": "code", "execution_count": 59, "id": "7aafcf1c", "metadata": {}, "outputs": [], "source": [ "def hi(*arguments): \n", " return 'hi'+ str(arguments)" ] }, { "cell_type": "code", "execution_count": 60, "id": "11abcb2a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hi('sahil',)\n", "hi('sahil', 'sourav')\n" ] } ], "source": [ "print(hi('sahil'))\n", "print(hi('sahil','sourav'))" ] }, { "cell_type": "markdown", "id": "d823662c", "metadata": {}, "source": [ "### When you are unsure about the number of arguments and you don't want to assign arguments in advance - For keyword arguments" ] }, { "cell_type": "markdown", "id": "a970ac32", "metadata": {}, "source": [ "- Use double asterick\n", "- **kwargs\n", "- **kwargs is dictinary by default\n", "- Note: kwargs (or **) only picks up the = arguments\n", "- common usecase is when u wrote one function for superclass and in subclasses you need only few arguments" ] }, { "cell_type": "code", "execution_count": 62, "id": "065a50d3", "metadata": {}, "outputs": [], "source": [ "def hi(**arguments):\n", " return 'hi'+ str(arguments)" ] }, { "cell_type": "code", "execution_count": 64, "id": "fead4b03", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "hi() takes 0 positional arguments but 1 was given", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m<ipython-input-64-25353f36f7a9>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhi\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'sahil'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# it will give error :hi() takes 0 positional arguments but 1 was given because\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;31m# kwargs (or **) only picks the values passed as =\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mTypeError\u001b[0m: hi() takes 0 positional arguments but 1 was given" ] } ], "source": [ "print(hi('sahil')) # it will give error :hi() takes 0 positional arguments but 1 was given because\n", "# kwargs (or **) only picks the values passed as =" ] }, { "cell_type": "code", "execution_count": 65, "id": "ec03a350", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hi{'name': 'sahil'}\n" ] } ], "source": [ "print(hi(name='sahil'))" ] }, { "cell_type": "code", "execution_count": 67, "id": "e3dc38bc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hi{'name1': 'sahil', 'name2': 'sourav'}\n" ] } ], "source": [ "print(hi(name1='sahil',name2='sourav'))" ] }, { "cell_type": "markdown", "id": "dd6011bb", "metadata": {}, "source": [ "### Summary (All used in one Example)" ] }, { "cell_type": "code", "execution_count": 75, "id": "e7ac7025", "metadata": {}, "outputs": [], "source": [ "def hi(a,b,*args,**kwargs):\n", " return 'hi '+str(a)+ str(b)+ str(args)+ str(kwargs)" ] }, { "cell_type": "code", "execution_count": 76, "id": "0b5c8c85", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hi 1020(){}'" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hi(10,20) # both these will be picked by positional arguments (a,b) only \n", "# also,if you even miss one value and pass only one value,it will throw error.\n", "# all the positional arguments must be passed always.This is the rule" ] }, { "cell_type": "code", "execution_count": 77, "id": "c4d7adc4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hi 1020(30, 40){}'" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hi(10,20,30,40) # after taking and b from arguments,rest everything will be taken as args\n", "# and since args comes as tupple by default,they will be in tupple" ] }, { "cell_type": "code", "execution_count": 78, "id": "f784224e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hi 1020(30, 40, 50, 60){}'" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hi(10,20,30,40,50,60) # after taking and b from arguments,rest everything will be taken as args\n", "# and since args comes as tupple by default,they will be in tupple\n", "# Note: No kwargs will be taken because nothing is passed as =" ] }, { "cell_type": "code", "execution_count": 80, "id": "abef001f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"hi 1020(30, 40, 50, 60){'x': 70, 'y': 80}\"" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hi(10,20,30,40,50,60,x=70,y=80) # x and y will be taken as kwars since they value =\n", "# and kwargs comes as dictionary by default" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }